package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.ShopCarAdapter;
import com.dingwei.superiordiary.bean.ShopCartListBean;
import com.dingwei.superiordiary.mvp.presenter.ShopCartPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.utils.NumberUtil;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.L;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/5/7.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */
public class ShopCartActivity extends BaseActivity implements Contract.ShopCartView {

    ShopCartPresenter shopCartPresenter = new ShopCartPresenter(this);

    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.data_layout)
    LinearLayout dataLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;

    ShopCarAdapter adapter;
    @Bind(R.id.img_check)
    ImageView imgCheck;
    @Bind(R.id.tv_total_price)
    TextView tvTotalPrice;
    @Bind(R.id.btn_submit)
    Button btnSubmit;
    @Bind(R.id.check_layout)
    LinearLayout checkLayout;

    private boolean isAllCheck;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_shop_cart;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        shopCartPresenter.getShopCartList();
    }

    @Override
    protected void initView() {

        tvTitle.setText("购物车");

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.line), 1));
        adapter = new ShopCarAdapter(BaseActivity.context);
        mRecyclerView.setAdapter(adapter);

        adapter.setOnShopCartInterFaceListener(new ShopCarAdapter.onShopCartInterFace() {
            @Override
            public void add(int storePosition, int position) {
                ShopCartListBean.DataBean.ProductsBean productsBean = bean.data.get(storePosition).products.get(position);
                int num = productsBean.quantity + 1;
                shopCartPresenter.editCartNum(storePosition, position, productsBean.id, num);
            }

            @Override
            public void reduce(int storePosition, int position) {
                ShopCartListBean.DataBean.ProductsBean productsBean = bean.data.get(storePosition).products.get(position);
                int num = productsBean.quantity - 1;
                shopCartPresenter.editCartNum(storePosition, position, productsBean.id, num);
            }

            @Override
            public void check(int storePosition, int position) {
                ShopCartListBean.DataBean.ProductsBean productsBean = bean.data.get(storePosition).products.get(position);
                productsBean.isSelect = !productsBean.isSelect;

                boolean b = true;
                for (ShopCartListBean.DataBean.ProductsBean product : bean.data.get(storePosition).products) {
                    if (!product.isSelect) {
                        b = false;
                        break;
                    }
                }
                bean.data.get(storePosition).isSelect = b;

                adapter.notifyItemChanged(storePosition);
                refrshtotalPrice();
                refreshAllCheck();
            }

            @Override
            public void delete(int storePosition, int position) {
                ShopCartListBean.DataBean.ProductsBean productsBean = bean.data.get(storePosition).products.get(position);
                shopCartPresenter.delete(storePosition, position, productsBean.id);
            }

            @Override
            public void storeCheck(int position) {
                ShopCartListBean.DataBean dataBean = bean.data.get(position);
                dataBean.isSelect = !dataBean.isSelect;
                for (ShopCartListBean.DataBean.ProductsBean product : dataBean.products) {
                    product.isSelect = dataBean.isSelect;
                }
                adapter.notifyItemChanged(position);
                refrshtotalPrice();
                refreshAllCheck();
            }
        });

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    ShopCartListBean bean;

    @Override
    public void setData(ShopCartListBean bean) {
        this.bean = bean;
        adapter.clear();
        if (bean.data != null && bean.data.size() > 0) {
            adapter.addAll(bean.data);
        }
        refreshAllCheck();
        refrshtotalPrice();
        showData("success");
    }

    @Override
    public void onFail() {
        showData("fail");
    }

    @Override
    public void editCartNumSuccess(int storePosition, int position, int num) {
        ShopCartListBean.DataBean.ProductsBean productsBean = bean.data.get(storePosition).products.get(position);
        productsBean.quantity = num;
        productsBean.total_price = productsBean.price * num;
        adapter.notifyItemChanged(storePosition);
        refrshtotalPrice();
    }

    @Override
    public void deleteSuccess(int storePosition, int position) {
        bean.data.get(storePosition).products.remove(position);
        if (bean.data.get(storePosition).products == null || bean.data.get(storePosition).products.size() <= 0) {
            adapter.remove(storePosition);
        }
        adapter.notifyDataSetChanged();
        showData("success");
    }


    public void showData(String state) {
        if (state.equals("success")) {
            noNetLayout.setVisibility(View.GONE);
            dataLayout.setVisibility(View.VISIBLE);
            if (adapter.getAllData() != null && adapter.getAllData().size() > 0) {
                mRecyclerView.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            } else {
                mRecyclerView.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
                isAllCheck = false;
                imgCheck.setImageResource(R.drawable.ic_unsele);
                tvTotalPrice.setText("合计：￥0");
            }
        } else {
            dataLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.img_back, R.id.btn_submit, R.id.check_layout, R.id.no_net_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.btn_submit:

                String car_ids = "";

                for (ShopCartListBean.DataBean dataBean : adapter.getAllData()) {
                    for (ShopCartListBean.DataBean.ProductsBean product : dataBean.products) {
                        if (product.isSelect){
                            if (TextUtils.isEmpty(car_ids)){
                                car_ids = product.id ;
                            }else {
                                car_ids = car_ids + "," + product.id;
                            }
                        }
                    }
                }

                if (TextUtils.isEmpty(car_ids)){
                    showToast("请选择要下单的商品");
                }else {
                    car_ids = "["+car_ids + "]";
                    intent = new Intent(BaseActivity.context,ConfirmOrderActivity.class);
                    intent.putExtra("car_ids",car_ids);
                    startActivity(intent);
                }

                break;
            case R.id.check_layout:
                if (bean.data!=null&&bean.data.size()>0){
                    isAllCheck = !isAllCheck;
                    for (int i = 0; i < bean.data.size(); i++) {
                        bean.data.get(i).isSelect = isAllCheck;
                        for (ShopCartListBean.DataBean.ProductsBean product : bean.data.get(i).products) {
                            product.isSelect = isAllCheck;
                        }
                    }
                    adapter.notifyDataSetChanged();
                    refrshtotalPrice();
                    refreshAllCheck();
                }
                break;
            case R.id.no_net_btn:
                shopCartPresenter.getShopCartList();
                break;
        }
    }

    public void refreshAllCheck() {
        if (bean.data!=null&&bean.data.size()>0){
            boolean b = true;
            for (int i = 0; i < bean.data.size(); i++) {
                for (ShopCartListBean.DataBean.ProductsBean product : bean.data.get(i).products) {
                    if (!product.isSelect) {
                        b = false;
                        break;
                    }
                }
            }
            isAllCheck = b;
            if (isAllCheck) {
                imgCheck.setImageResource(R.drawable.ic_sele);
            } else {
                imgCheck.setImageResource(R.drawable.ic_unsele);
            }
        }

    }

    public void refrshtotalPrice() {

        if (bean.data!=null&&bean.data.size()>0){
            double totalPrice = 0;
            for (int i = 0; i < bean.data.size(); i++) {
                for (ShopCartListBean.DataBean.ProductsBean product : bean.data.get(i).products) {
                    if (product.isSelect) {
                        totalPrice = totalPrice + product.total_price;
                    }
                }
            }
            tvTotalPrice.setText("合计：￥" + NumberUtil.getAmountValue(totalPrice));
        }


    }
}
